package com.example.leoss.duanxingxufa;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatImageView;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leoss.R;

import java.util.ArrayList;
import java.util.List;

public class DxAdpate extends BaseAdapter {

    ArrayList<DataDx> list;
        Context  mcontent;
         boolean inten=true;


    boolean mIsBeingDragged = false;
    ViewHolder holder;
    private boolean isShowCheckBox = false;//表示当前是否是多选状态。
    private  boolean isshowImage=false;
    private SparseBooleanArray stateCheckedMap = new SparseBooleanArray();//用来存放CheckBox的选中状态，true为选中,false为没有选中
    private  SparseBooleanArray stateImagemap=new SparseBooleanArray();

    public DxAdpate(ArrayList<DataDx> list, Context mcontent,SparseBooleanArray stateCheckedMap ,SparseBooleanArray stateImagemap) {
        this.list = list;
        this.mcontent = mcontent;
        this.stateCheckedMap=stateCheckedMap;
        this.stateImagemap=stateImagemap;
    }

    public DxAdpate() {
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"ViewHolder", "WrongConstant", "ClickableViewAccessibility", "WrongViewCast"})

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mcontent, R.layout.layout_msg, null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
       // convertView = LayoutInflater.from(mcontent).inflate(R.layout.layout_msg, null);

        holder.checkBox = (AppCompatCheckBox) convertView.findViewById(R.id.chb_select_way_point);

        holder.name = convertView.findViewById(R.id.id_layout_msg_tv_name);
        holder.phome = convertView.findViewById(R.id.id_layout_msg_tv_phone);
        holder.content = convertView.findViewById(R.id.id_layout_msg_tv_content);
        holder.id = convertView.findViewById(R.id.id_layout_msg_tv_id);
        holder.imageView=convertView.findViewById(R.id.img_yifasong);
           // final ImageView imageView = convertView.findViewById(R.id.id_duanxin_xianshi);
        holder.content .setMovementMethod(ScrollingMovementMethod.getInstance());

        showAndHideCheckBox();//控制CheckBox的那个的框显示与隐藏
        //showImage();
        holder.content .setOnTouchListener(new View.OnTouchListener() {
                @Override

                public boolean onTouch(View v, MotionEvent event) {
                    final int action = event.getAction();     //这个滑动事件没有问题                mIsBeingDragged这个???? 接下来我们看看是什么意思    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {        return true;    }
                    if(event.getAction()==MotionEvent.ACTION_DOWN){
                        //通知父控件不要干扰
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    if(event.getAction()==MotionEvent.ACTION_MOVE){
                        //通知父控件不要干扰
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                    }
                    if(event.getAction()==MotionEvent.ACTION_UP){
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                    }
                    return false;
                }


            });

        holder.checkBox.setChecked(stateCheckedMap.get(position));//设置CheckBox是否选中
       // holder.imageView.set
            if (stateImagemap.get(position)){
                holder.imageView.setVisibility(View.VISIBLE);
            }else {
                holder.imageView.setVisibility(View.GONE);
            }


        holder. name.setText(list.get(position).getName());
        holder.id.setText(list.get(position).getId());
        holder. phome.setText(list.get(position).getPhome());
        holder. content.setText(list.get(position).getContent());




        /**
         * 数据读取
         */
        SharedPreferences sharedPreferences= mcontent.getSharedPreferences("data",Context.MODE_PRIVATE);
        int h=sharedPreferences.getInt("h",50);
        int is=sharedPreferences.getInt("is",0);
        holder.content.setHeight(h);
        holder.content.setVisibility(is);

        return convertView;
    }


    public class ViewHolder {
        public TextView name,phome,content,id;
        public AppCompatCheckBox checkBox;
        public AppCompatImageView imageView;
    }

    private void showAndHideCheckBox() {
        if (isShowCheckBox) {
            holder.checkBox.setVisibility(View.VISIBLE);
        } else {
            holder.checkBox.setVisibility(View.GONE);
        }
    }


//private  void  showImage(){
//
//    if (isshowImage) {
//        holder.imageView.setVisibility(View.VISIBLE);
//    } else {
//        holder.imageView.setVisibility(View.GONE);
//    }
//}



public  boolean ishowImage(){
        return isshowImage;
}
    public  void setishowImage(boolean isshowImage){
        this.isshowImage=isshowImage;
    }




    public boolean isShowCheckBox() {
        return isShowCheckBox;
    }


    public void setShowCheckBox(boolean showCheckBox) {
        isShowCheckBox = showCheckBox;
    }
}
